<?php
/**
 * 发货通知单
 * Created by PhpStorm.
 * User: echa
 * Date: 2018/3/21
 * Time: 16:08
 */
namespace App\Http\Controllers\WarehouseManage;
use App\Http\Controllers\ApiController;
use App\Services\Warehouse\ConsignmentNoteService;
use App\Services\Warehouse\SalesReleaseService;
use Illuminate\Http\Request;

class ConsignmentNoteController extends ApiController{
    protected $ConsignmentNoteService;
    protected $SalesReleaseService;
    public function __construct(ConsignmentNoteService $consignmentNoteService,SalesReleaseService $salesReleaseService)
    {
        $this->ConsignmentNoteService=$consignmentNoteService;
        $this->SalesReleaseService=$salesReleaseService;
    }

    /***
     * 问题处理(缺货退货、次品退货、退货退款)
     * refund_data   参数：{"refund_type":1,"FBillNo":"FHTZD000028","goods":{"0080687504565":{"goods_sn":"0080687504565","op_number":3},"80687504":{"goods_sn":"80687504","op_number":5}}}
     * @param Request $request
    @author echa
     */
    public function UpdateConsignmentNote(Request $request){
        $refund_data=$request->refund_data;
        if(empty($refund_data)){
            return $this->failed('非法请求');
        }
        $refund_data=json_decode($refund_data,true);
        if(empty($refund_data)){
            return $this->failed('参数错误');
        }
        $FBillNo=$refund_data['FBillNo'];
        $goods=$refund_data['goods'];
        $type=$refund_data['refund_type'];
        if(!$goods){
            return $this->failed('缺失商品数据');
        }
        if(empty($refund_data['note'])){
            $refund_data['note']='';
        }
        $arrdata=[
            'FBillNo'=>$FBillNo,
            'note'=>$refund_data['note'],
            'goods'=>$goods
        ];
        try{
            if($type==3){ //退货退款
                $return=$this->ConsignmentNoteService->UpdateConsignmentNote($arrdata);
            }else if ($type==2){//次品退款
                $return=$this->ConsignmentNoteService->UpdateConsignmentNoteAndDirectTransfers($arrdata);
            }else if($type==1){// 缺货退货
                $return=$this->ConsignmentNoteService->UpdateConsignmentNoteAndMisDelivery($arrdata);
            }else{
                return $this->failed('操作类型不存在');
            }
            if(empty($return['Id'])){
                return $this->failed('操作失败');
            }else{
                return $this->success($return);
            }
        }catch (\Exception $exception){
            return $this->failed($exception->getMessage());
        }
    }

    /***
     * 更改发货通知单为已审核状态
     * @param Request $request
    @author echa
     * @return mixed
     */
    public function SubmitConsignmentNote(Request $request){
        $FBillNo=$request->k3cn_sn;
        if(empty($FBillNo)){
            return $this->failed('非法请求');
        }
        try{
            $return=$this->ConsignmentNoteService->ConsignmentNoteSubmitAndAudit([$FBillNo]);
            if(empty($return['Id'])){
                return $this->failed('操作失败');
            }else{
                return $this->success($return);
            }
        }catch (\Exception $exception){
            return $this->failed($exception->getMessage());
        }
    }

    /***
     * 发货通知单下推销售出库单
     * 参数  numbers  JSON格式："["FHTZD000035","FHTZD000036","FHTZD000037"]"
     * @param Request $request
    @author echa
     */
    public function ConsignmentNotePushSalesRelease(Request $request){
        $Numbers=$request->numbers;
        if(empty($Numbers)){
            return $this->failed('非法请求');
        }
        $array_numbers=json_decode($Numbers,true);
        if(empty($array_numbers)){
            return $this->failed('参数错误');
        }
        try{
            $return=$this->ConsignmentNoteService->ConsignmentNotePushSalesRelease($array_numbers);
            if(empty($return['Id'])){
                return $this->failed('操作失败');
            }else{
                $submitreturn=$this->SalesReleaseService->SubmitSalesRelease([$return['Number']]);
                if(empty($submitreturn['Id'])){
                    return $this->failed('下推销售出库单提交失败');
                }
                $auditreturn=$this->SalesReleaseService->AuditSalesRelease([$return['Number']]);
                if(empty($auditreturn['Id'])){
                    return $this->failed('下推销售出库单审核失败');
                }
                return $this->success($auditreturn);
            }
        }catch (\Exception $exception){
            return $this->failed($exception->getMessage());
        }
    }

    /***
     * 检查发货通知单是否作废
     * 参数说明
     * k3cn_sn  发货通知单编号
     * @param Request $request
    @author echa
     * @return mixed
     */
    public function CheckConsignmentNoteCancelStatus(Request $request){
        $FBillNo=$request->k3cn_sn;
        if(empty($FBillNo)){
            return $this->failed('非法请求');
        }
        try{
            $return=$this->ConsignmentNoteService->CheckConsignmentNoteDocumentStatusAndCancelStatus($FBillNo);
            if($return){
                if($return=='A'){
                    return $this->success(true);
                }else{
                    return $this->success(false);
                }
            }else{
                return $this->failed('查无此发货通知单');
            }
        }catch (\Exception $exception){
            return $this->failed($exception->getMessage());
        }
    }

    /***
     * 发货通知单并且提交审核并且下推为销售出库单
     * 参数说明
     * k3cn_sn=""
     * @param Request $request
    @author echa
     * @return mixed
     */
    public function ConsignmentNoteSubmitAndPushSalesRelease(Request $request){
        $Number=$request->k3cn_sn;
        if(empty($Number)){
            return $this->failed('非法请求');
        }
        try{
            $return=$this->ConsignmentNoteService->ConsignmentNoteSubmitAndPushSalesRelease($Number);
            if($return){
                $submitreturn=$this->SalesReleaseService->SubmitSalesRelease([$return['Number']]);
                if(empty($submitreturn['Id'])){
                    return $this->failed('下推销售出库单提交失败');
                }
                $auditreturn=$this->SalesReleaseService->AuditSalesRelease([$return['Number']]);
                if(empty($auditreturn['Id'])){
                    return $this->failed('下推销售出库单审核失败');
                }
                return $this->success($auditreturn);
            }else{
                return $this->failed('操作失败');
            }
        }catch (\Exception $exception){
            return $this->failed($exception->getMessage());
        }

    }

    /***
     * 发货通知单操作反审核以及作废单据
     * @param Request $request
    @author echa
     * @return mixed
     */
    public function UnAuditAndCancelConsignmentNote(Request $request){
        $Number=$request->k3cn_sn;
        if(empty($Number)){
            return $this->failed('非法请求');
        }
        try{
            $return=$this->ConsignmentNoteService->UnAuditAndCancelConsignmentNote([$Number]);
            if($return){
                return $this->success($return);
            }else{
                return $this->failed('操作失败');
            }
        }catch (\Exception $exception){
            return $this->failed($exception->getMessage());
        }
    }
}